// pages/examineOnlineDetails/examineOnlineDetails.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    id:'',
    communityList: [],
    communityIndex: 999,
    CardRecharge:[],
    CardRechargeIndex:999,
    cardDeposit:[],
    cardDepositIndex: 999,
    disabled:false,
    statusList: [
      {
        id: 1,
        label: '审核中'
      }, {
        id: 2,
        label: '审核通过'
      }, {
        id: 3,
        label: '审核拒绝'
      }
    ],
    statusIndex: 999,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    wx.showLoading();
    this.getInfo();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  // 选择园区
  communityChange: function(e) {
    let province_name = this.data.communityList[e.detail.value].province_name,
        city_name = this.data.communityList[e.detail.value].city_name,
        area_name = this.data.communityList[e.detail.value].area_name,
        address_name = this.data.communityList[e.detail.value].address;
    let address = '';
    if (province_name == city_name){
      address = province_name + area_name + address_name;
    }else{
      address = province_name + city_name + area_name + address_name;
    }
    console.log(address)
    this.setData({
      communityIndex: e.detail.value,
      'userInfo.address': address
    })
  },

  // 选择充值金额
  rechargeChange: function(e) {
    this.setData({
      CardRechargeIndex: e.detail.value
    })
  },

  // 选择押金金额
  depositChange: function(e) {
    this.setData({
      cardDepositIndex: e.detail.value
    })
  },

  // 选择办理状态
  handlestate: function(e) {
    var that = this;
    that.setData({
      statusIndex: e.detail.value
    })
    let type = '';
    if (that.data.statusIndex!=999){
      type = that.data.statusList[that.data.statusIndex].id;
    }
    app.ajax({
      url: 'Partner/WaterCard/operationAudit',
      data: {
        id: that.data.id,
        type: type,
        sn: that.data.userInfo.card_number
      },
      success: function (res) {
        //console.log(res)
        app.showToast(res.data.message);
      }
    })
  },
  //改卡号
  sn:function(e){
    this.setData({
      'userInfo.card_number': e.detail.value
    })
  },
  // 确认
  formSubmit: function(e) {
    console.log(e.detail.value)
    var info = e.detail.value;
    var that = this;

    let village_name = '',
      province = '',
      city = '',
      area = '',
      cardRecharge='',
      cardDeposit='';

    if (that.data.communityIndex!=999){
      village_name = that.data.communityList[that.data.communityIndex].village_name;
      province = that.data.communityList[that.data.communityIndex].province;
      city = that.data.communityList[that.data.communityIndex].city;
      area = that.data.communityList[that.data.communityIndex].area;
    }
    if (that.data.CardRechargeIndex != 999) {
      cardRecharge = that.data.CardRecharge[that.data.CardRechargeIndex];
    }
    if (that.data.cardDepositIndex != 999) {
      cardDeposit = that.data.cardDeposit[that.data.cardDepositIndex];
    }


    app.ajax({
      url: 'Partner/WaterCard/edit_water_apply',
      data: {
        id: that.data.id,
        village_name: village_name, 
        card_number: info.cardnum, 
        full_name: info.username, 
        tel: info.phonenum, 
        IDcard: info.idnum, 
        address: info.address, 
        province: province, 
        city: city, 
        area: area,
        cardDeposit: cardDeposit,
        cardRecharge: cardRecharge
      },
      success: function (res) {
        //console.log(res)
        app.showToast(res.data.message);
        if (res.data.code == 1000) {
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    })
  },

  // 返回
  returnBtn:function(){
    wx.navigateBack({
      delta: 1,
    })
  },
  //获取信息
  getInfo:function(){
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/getAccountWaterCardAudit',
      data: {
        id: that.data.id
      },
      success: function (res) {
        //console.log(res)
        wx.hideLoading();
        if (res.data.code == 1000) {
          let statusIndex = that.data.statusIndex;
          for (let i = 0; i < that.data.statusList.length;i++){
            if (res.data.data.audit_status_tmp == that.data.statusList[i].id){
              statusIndex = i;
            }
          }


          that.setData({
            userInfo: res.data.data,
            statusIndex
          })
          that.getVillage(res.data.data.village_id);
          that.getCardRecharge(res.data.data.recharge_money);
          that.getCardDeposit(res.data.data.card_deposit)
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //获取园区列表
  getVillage(village_id) {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/get_village',
      data: {
        village_name: ''
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          let villageList = res.data.data;
          let villageIndex = that.data.communityIndex;
          for (let i = 0; i < villageList.length; i++) {
            if (village_id && village_id == villageList[i].id) {
              villageIndex = i
            }
          }
          that.setData({
            communityList: villageList,
            communityIndex: villageIndex
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //获取水卡充值金额
  getCardRecharge(recharge_money){
    var that = this;
    app.ajax({
      url: 'Common/Common/waterCardRecharge',
      data: {},
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          let CardRecharge = [];
          CardRecharge.push(res.data.data)
          let CardRechargeIndex = that.data.CardRechargeIndex;
          for (let i = 0; i < CardRecharge.length; i++) {
            if (recharge_money && parseInt(recharge_money) == parseInt(CardRecharge[i])) {
              CardRechargeIndex = i
            }
          }
          that.setData({
            CardRecharge: CardRecharge,
            CardRechargeIndex: CardRechargeIndex
          })
        } 
      }
    })
  },
  //获取水卡押金
  getCardDeposit(card_deposit) {
    var that = this;
    app.ajax({
      url: 'Common/Common/cardDeposit',
      data: {},
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          let cardDeposit = [];
          cardDeposit.push(res.data.data)
          let cardDepositIndex = that.data.cardDepositIndex;
          for (let i = 0; i < cardDeposit.length; i++) {
            if (card_deposit && parseInt(card_deposit) == parseInt(cardDeposit[i])) {
              cardDepositIndex = i
            }
          }
          that.setData({
            cardDeposit: cardDeposit,
            cardDepositIndex: cardDepositIndex
          })
        }
      }
    })
  }
})